package com.dhecp.project.system.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.config.Global;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.common.utils.file.FileUploadUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.enums.SQLConditionsTypeSimple;
import com.dhecp.project.system.domain.SysSetting;
import com.dhecp.project.system.service.SysSettingService;

/**
 * 系统设置 控制层
 * @author lwl
 * @version 2019年10月31日
 */
@Controller
@RequestMapping("/system/setting")
public class SysSettingController extends BaseController {

	private String prefix = "system/setting";
    private String moduleTable = ModuleTable.SYSTEM_SETTINGS;
    
    @Autowired
    private SysSettingService settingService;
   
    
    /**
     * 打开系统设置列表
     * @param 
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("system:setting:view")
    @GetMapping()
    public ModelAndView List(@RequestParam Map<String, Object> params){
        ModelAndView mv = this.getModelAndView();
        Map<String,Object> dataLimitMap = TypeUtils.EnumToMap(SQLConditionsTypeSimple.class);
        mv.addObject("dataLimitMap", dataLimitMap); //查询条件下拉框数据
        mv.setViewName(prefix + "/setting");
        return mv;
    }
    
    /**
     * 打开用户修改页
     * @return ModelAndView
     * @author lwl
     */
    @RequiresPermissions("system:setting:edit")
    @GetMapping("/edit")
    public ModelAndView edit(){
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String,Object> dataLimitMap = TypeUtils.EnumToMap(SQLConditionsTypeSimple.class);
        SysSetting systemSetting = new SysSetting();
        if (mv.isEmpty()){
            try{
            	systemSetting = settingService.querySetting();
                if (systemSetting == null){
                	ajaxResult = AjaxResult.error("没有查到系统信息。");
                }else{
                    ajaxResult = AjaxResult.success("数据获取成功。");
                }
            }
            catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            mv.addObject("pageType","edit");
            mv.addObject("systemSetting",systemSetting);
            mv.addObject("dataLimitMap", dataLimitMap); //查询条件下拉框数据
            mv.addObject("ajaxResult", ajaxResult);
            mv.setViewName(prefix + "/setting");
        }
        return mv;
    }
    /**
     * 打开修改系统Logo页
     * @return ModelAndView
     * @author kimiloo
     */
    @RequiresPermissions("system:setting:edit")
    @GetMapping("/systemlogo")
    public ModelAndView systemlogo(){
    	ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        SysSetting systemSetting = new SysSetting();
        if (mv.isEmpty()){
            try{
            	systemSetting = settingService.querySetting();
                if (systemSetting == null){
                	ajaxResult = AjaxResult.error("没有查到系统信息。");
                }else{
                    ajaxResult = AjaxResult.success("数据获取成功。");
                }
            }
            catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            mv.addObject("systemSetting",systemSetting);
            mv.addObject("ajaxResult", ajaxResult);
            mv.setViewName(prefix + "/upLogo");
        }
        return mv;
    }
    
    /**
     * 修改系统设置——数据提交
     * @param settingItems 必要参数：对应实体类的json格式字符串
     * @return AjaxResult
     * @author lwl
     */
    @RequiresPermissions("system:setting:edit")
    @Log(title = "系统设置修改", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (params.get("settingItems") == null || params.get("settingItems") == ""){
        	return AjaxResult.error("缺少参数settingItems【对应实体类的json格式字符串】。");
        }
        try {
            ajaxResult= settingService.updateSetting(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    /***
     * 修改系统Logo
     * @param avatarfile 文件
     * @return AjaxResult
     * @author kimiloo
     */
 	@Log(title = "修改系统Logo", businessType = BusinessType.UPDATE)
 	@PostMapping("/updateLogo")
 	@ResponseBody
 	public AjaxResult updateLogo(@RequestParam("logofile") MultipartFile uploadfile){
 		Map<String, Object> params = new HashMap<String, Object>();
 		try{
 	    	if (!uploadfile.isEmpty()){
 	            String fileDirPath = Global.getUploadPath();
 	            fileDirPath = Global.getProfile() + "/systemlogo";
 	            String filePath = FileUploadUtils.upload(fileDirPath, uploadfile);
 	            
 	            SysSetting systemSetting = new SysSetting();
 	            systemSetting = settingService.querySetting();
 	            
 	            params.put("guid", systemSetting.getGuid());
 	            params.put("systemLogo", filePath);
 	            AjaxResult ajaxResult = new AjaxResult();
 	            try {
 	                ajaxResult= settingService.updateSettingLogo(params);
 	            }catch (Exception e) {
 	                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
 	            }
 	            return ajaxResult;
 	       }
 	       return error();
 		}
 		catch (Exception e)
 		{
 	       return error(e.getMessage());
 		}
 	}
}
